package org.lex.ftp.ui;

import java.awt.Component;

import javax.swing.*;

import org.lex.ftp.FTP;
import org.lex.ftp.util.Actions;
import org.lex.ftp.util.Resources;
import org.lex.ftp.util.bookmark.BookmarkUtils;


public class MainToolBar extends JToolBar {
	private final FTP app;
	private final ActionMap am;

	public MainToolBar(FTP app) {
		super("Operations");
		this.app = app;
		this.am = app.getActionMap();
		this.initialize();
	}

	private void initialize() {
		JToggleButton button = MenuButton.create(new Popup());
		button.setIcon(Resources.getIcon(Resources.ConnectIcon));
		this.add(button);
		// ======================================
		this.add(am.get(Actions.DISCONNECT));
		this.addSeparator();
		this.add(am.get(Actions.TRANSFER));
		this.add(am.get(Actions.ABORT));
	}

	private class Popup extends JPopupMenu {
		private final JMenuItem connect;

		public Popup() {
			connect = new JMenuItem(am.get(Actions.CONNECT));
			connect.setIcon(null);
		}

		public void show(Component invoker, int x, int y) {
			this.prepareMenuItem();
			super.show(invoker, x, y);
		}

		private void prepareMenuItem() {
			this.removeAll();
			this.add(connect);
			this.addSeparator();
			for (JMenuItem item : BookmarkUtils.getBookmarkMenu(app))
				this.add(item);
		}
	}
}
